import tkinter as tk
import smtplib
from email.mime.text import MIMEText
from email.header import Header

def send_email():
    # 发件人邮箱，修改为 412023592@qq.com
    sender = '412023592@qq.com'
    # 收件人邮箱，这里假设收件人还是 412023592@qq.com，可按需修改
    receivers = ['412023592@qq.com']

    # 邮件内容
    message = MIMEText('helloworld', 'plain', 'utf-8')
    # 邮件标题
    message['Subject'] = Header('helloworld', 'utf-8')
    # 发件人，修改为直接使用邮箱地址
    message['From'] = sender
    # 收件人，修改为直接使用邮箱地址
    message['To'] = ','.join(receivers)

    smtpObj = None
    try:
        # 连接到QQ邮箱的SMTP服务器，端口使用 587 （QQ邮箱建议使用该端口并开启TLS加密）
        smtpObj = smtplib.SMTP('smtp.qq.com', 587)
        # 开启TLS加密
        smtpObj.starttls()
        # 登录邮箱，这里需要替换为你的QQ邮箱授权码
        smtpObj.login(sender, 'wbrkciotqitkbich')
        # 发送邮件
        smtpObj.sendmail(sender, receivers, message.as_string())
        print("邮件发送成功")
    except smtplib.SMTPException as e:
        print(f"Error: 无法发送邮件，错误信息: {e}")
    finally:
        if smtpObj:
            try:
                # 关闭连接
                smtpObj.quit()
            except smtplib.SMTPServerDisconnected:
                pass

# 创建主窗口
root = tk.Tk()
root.title("邮件发送软件")

# 创建发送按钮
send_button = tk.Button(root, text="发送", command=send_email)
send_button.pack(pady=20)

# 运行主循环
root.mainloop()
